package com.manage.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.manage.platform.dto.DictValueDto;
import com.manage.platform.dto.query.DictValuePageQuery;
import com.manage.platform.entity.DictValueEntity;
import com.manage.platform.exception.CommonException;
import com.manage.platform.exception.EmError;
import com.manage.platform.mapper.DictValueMapper;
import com.manage.platform.utils.RPage;
import com.manage.platform.vo.DictPartVo;
import com.manage.platform.vo.DictValueVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * YWD
 * 2025/9/25
 */
@AllArgsConstructor
@Service
public class DictValueServiceImpl extends ServiceImpl<DictValueMapper, DictValueEntity> {


    public RPage<List<DictValueVo>> getDictValuePage(DictValuePageQuery query) {
        final Page<Object> page = PageHelper.startPage(query.getPage(), query.getLimit());
        List<DictValueVo> list = this.baseMapper.getDictValuePage(query);
        return RPage.ok(list, query.getPage(), query.getLimit(), page.getTotal());
    }

    public DictValueVo getDictValueDetails(Long id) {
        return BeanUtil.copyProperties(getById(id), DictValueVo.class);
    }

    public Boolean saveDictValue(DictValueDto dto) {
        //是否重复
        LambdaQueryWrapper<DictValueEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DictValueEntity::getDictId, dto.getDictId())
                .eq(DictValueEntity::getValue, dto.getValue());
        List<DictValueEntity> list = list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            throw new CommonException("字典值已存在");
        }
        return save(BeanUtil.copyProperties(dto, DictValueEntity.class));
    }

    public Boolean updateDictValue(DictValueDto dto) {
        //是否重复
        LambdaQueryWrapper<DictValueEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DictValueEntity::getDictId, dto.getDictId())
                .eq(DictValueEntity::getValue, dto.getValue())
                .ge(DictValueEntity::getId, dto.getId());
        List<DictValueEntity> list = list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            throw new CommonException("字典值已存在");
        }
        return updateById(BeanUtil.copyProperties(dto, DictValueEntity.class));
    }

    public Boolean deleteDictValue(Long id) {
        //是否正在使用中
        return removeById(id);
    }

    public void removeByDictId(Long dictId) {
        LambdaUpdateWrapper<DictValueEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(DictValueEntity::getDictId, dictId);
        remove(updateWrapper);
    }

    public List<DictPartVo> getDictPartList(String dictName) {
        return this.baseMapper.getDictPartList(dictName);
    }
}
